import java.awt.Graphics2D;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Menu {

	private ArrayList items;
	private Image background;
	private int x, y;
	private Game parent;

	public Menu(int x, int y, Game parent) {
		this.x = x;
		this.y = y;
		items = new ArrayList();

		this.parent = parent;
	}

	/**
	 * Returns an int representing the x-position of the menu
	 * 
	 * @return the x-position of the menu
	 */
	public int getX() {
		return x;
	}

	/**
	 * Returns an int representing the y-position of the menu
	 * 
	 * @return the y-position of the menu
	 */
	public int getY() {
		return y;
	}

	@SuppressWarnings("unchecked")
	/**
	 * Adds an items to the menu.
	 * @param x x-position, relative to the parent menu
	 * @param y y-position, relative to the parent menu
	 * @param bgPath the location of the background image file
	 */
	public void createItem(int x, int y, String bgPath) {
		items.add(new MenuItem(this, x + this.x, y + this.y, 0, bgPath));
	}

	@SuppressWarnings("unchecked")
	/**
	 * Adds an item to the menu on the same x-position and 20px below the last item.
	 * @param bgPath the location of the background image file
	 */
	public void createNext(String bgPath) {
		items.add(new MenuItem(this, lastItem().getX(), lastItem().getHeight() + lastItem().getY() + 20, items.size(), bgPath));
	}

	/**
	 * Returns the latest addition to the menu.
	 * 
	 * 
	 * @return
	 */
	private MenuItem lastItem() {
		return (MenuItem) items.get(items.size() - 1);
	}

	/**
	 * Sets the background image of the menu
	 * 
	 * @param bgPath
	 *            the location of the background image file
	 */
	public void setBackground(String bgPath) {
		ImageIcon ii = new ImageIcon(this.getClass().getResource(bgPath));
		background = ii.getImage();
	}

	public void draw(Graphics2D g2d) {
		ArrayList menuItems = getItems();

		g2d.drawImage(getBackground(), getX(), getY(), parent);

		for (int i = 0; i < menuItems.size(); i++) {
			MenuItem tItem = (MenuItem) menuItems.get(i);
			g2d.drawImage(tItem.getBackground(), tItem.getX(), tItem.getY(), parent);
		}
	}

	/**
	 * Takes mouse input and returns the ID of the clicked item
	 * 
	 * @param x
	 *            x-position of the mouse
	 * @param y
	 *            y-position of the mouse
	 * @return returns the ID of the clicked item, -1 if no item was clicked.
	 */
	public int mouseClick(int x, int y) {
		for (int i = 0; i < items.size(); i++) {
			MenuItem tItem = (MenuItem) items.get(i);
			if ((x >= tItem.getX()) && (x <= tItem.getX() + tItem.getWidth()) && (y >= tItem.getY()) && (y <= tItem.getY() + tItem.getHeight())) {
				return tItem.getID();
			}
		}
		return -1;
	}

	/**
	 * Returns an Image object that can be painted.
	 * 
	 * @return the background image of the menu
	 */
	public Image getBackground() {
		return background;
	}

	/**
	 * Returns an ArrayList object with all menu items of the menu.
	 * 
	 * @return an ArrayList object with all menu items of the menu
	 */
	public ArrayList getItems() {
		return items;
	}
}
